package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Strategy;
import cn.wolfcode.domain.StrategyCatalog;
import cn.wolfcode.query.StrategyCatalogQuery;
import cn.wolfcode.repository.StrategyCatalogRepository;
import cn.wolfcode.service.IStrategyCatalogService;
import cn.wolfcode.service.IStrategyService;
import cn.wolfcode.util.DBHelper;
import cn.wolfcode.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {



    @Autowired
    private StrategyCatalogRepository strategyCatalogRepository;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IStrategyService strategyService;

    @Override
    public void save(StrategyCatalog strategyCatalog) {
        strategyCatalog.setId(null);
        strategyCatalogRepository.save(strategyCatalog);
    }

    @Override
    public void update(StrategyCatalog strategyCatalog) {
        strategyCatalogRepository.save(strategyCatalog);
    }

    @Override
    public void delete(String id) {
        strategyCatalogRepository.deleteById(id);
    }

    @Override
    public StrategyCatalog get(String id) {
        return strategyCatalogRepository.findById(id).orElse(null);

    }

    @Override
    public List<StrategyCatalog> findAll() {
        return strategyCatalogRepository.findAll();
    }

    @Override
    public Page<StrategyCatalog> query(StrategyCatalogQuery qo) {
        Query query = new Query();
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(template, StrategyCatalog.class,query,pageable);
    }

    @Override
    public List<CatalogVO> groupList() {
        List<CatalogVO> list = new ArrayList<>();
        //聚合查询
        TypedAggregation<StrategyCatalog> agg = Aggregation.newAggregation(StrategyCatalog.class,
                Aggregation.group("destName").
                        push("name").as("names").
                        push("id").as("ids")
        );
        AggregationResults<Map> result = template.aggregate(agg,Map.class);
        List<Map> datas = result.getMappedResults();

        for (Map data : datas) {
            CatalogVO catalogVO = new CatalogVO();
            catalogVO.setDestName(data.get("_id").toString());
            List<Object> names = (List<Object>) data.get("names"); //攻略分类名称的集合
            List<Object> ids = (List<Object>) data.get("ids");      //攻略分类id的集合


            List<StrategyCatalog> catalogList = new ArrayList<>();    //攻略分类对象

            for (int i = 0; i < names.size(); i++) {
                StrategyCatalog strategyCatalog = new StrategyCatalog();
                String name = names.get(i).toString();
                String id = ids.get(i).toString();
                strategyCatalog.setName(name);
                strategyCatalog.setId(id);
                catalogList.add(strategyCatalog);
            }
        catalogVO.setCatalog(catalogList);
        list.add(catalogVO);
    }
        return list;
    }

    @Override
    public List<StrategyCatalog> queryCatalogByDestId(String destId) {
        //所有指定destId的分类
        List<StrategyCatalog> list = strategyCatalogRepository.findByDestId(destId);

        //遍历分类
        for (StrategyCatalog catalog : list) {
            List<Strategy> sts =  strategyService.queryCatalogId(catalog.getId());
            catalog.setStrategies(sts);
        }


        return list;
    }

}
